//
//  HttpRequest.m
//  AFNetworkingDemo
//
//  Created by mobao on 16/4/15.
//  Copyright © 2016年 mobao. All rights reserved.
//

#import "HttpRequest.h"

//请求的主页地址
static NSString * const BaseUrl = @"https://api.weibo.com/";

@implementation HttpRequest

+(instancetype)sharedClient{
    
    static HttpRequest *client = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        client = [[HttpRequest alloc]initWithBaseURL:[NSURL URLWithString:BaseUrl]];
        
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/json",@"text/javascript",nil];
        
    });
    
    return client;
    
}

-(NSURLSessionDataTask*)getWithURLString:(NSString*)URLString
                 parameters:(id)parameters
                    showHUD:(BOOL)showHud
                    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                    failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure{
    
    if (showHud) {
        [SVProgressHUD show];
    }
    NSURLSessionDataTask *task = [super GET:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (showHud) {
            [SVProgressHUD dismiss];
        }
        
        success(task,responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (showHud) {
            [SVProgressHUD dismiss];
        }
        
        failure(task,error);
        
    }];
    return task;
}

-(NSURLSessionDataTask*)postWithURLString:(NSString *)URLString
                  parameters:(id)parameters
                     showHUD:(BOOL)showHud
                     success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                     failure:(void (^)(NSURLSessionDataTask * task, NSError *error))failure
                     {
    if (showHud) {
        [SVProgressHUD show];
    }
    
    NSURLSessionDataTask *task = [super POST:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (showHud) {
            [SVProgressHUD dismiss];
        }
        
        success(task,responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failure(task,error);
        
    }];
    return task;
}

-(NSURLSessionDataTask*)getWithURLString:(NSString*)URLString
                 parameters:(id)parameters
                    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                    failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure{
    
    NSURLSessionDataTask *task = [self getWithURLString:URLString parameters:parameters showHUD:YES success:success failure:failure];
    
    return task;
    
}


-(NSURLSessionDataTask*)postWithURLString:(NSString *)URLString
                  parameters:(id)parameters
                     success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                     failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
    
    NSURLSessionDataTask *task = [self postWithURLString:URLString  parameters:parameters  showHUD:YES success:success failure:failure];
    
    return task;
}

-(NSURLSessionDataTask*)requestWithURLString:(NSString *)URLString
                                  parameters:(id)parameters
                                        type:(HttpRequestType)type
                                     showHUD:(BOOL)showHud
                                  success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                                  failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
    switch (type) {

        case HttpRequestTypeGet:
            
            if (showHud) {
                NSURLSessionDataTask *task = [self getWithURLString:URLString parameters:parameters showHUD:YES success:success failure:failure];
                
                return task;
            }else{
                NSURLSessionDataTask *task = [self getWithURLString:URLString parameters:parameters success:success failure:failure];
                return task;
            }
            break;
            
        case HttpRequestTypePost:
            
            if (showHud) {
                
                NSURLSessionDataTask *task = [self postWithURLString:URLString parameters:parameters showHUD:YES success:success failure:failure];
                
                return task;
                
            }else{
                
                NSURLSessionDataTask *task = [self postWithURLString:URLString parameters:parameters success:success failure:failure];
                
                return task;
                
            }
        default:
            break;
    }
    return nil;
    
}


@end
